﻿CREATE PROCEDURE [dbo].[RepriceDocInsert]
(--переоценка товара
	@Id int output,
	@DocDate smalldatetime,
	@DocNumber varchar(20) output,
	@Note varchar(250),
	@MD datetime output,
	@MW varchar(50)
)
AS
DECLARE @DepartmentUserId int, @intRet int
	SET NOCOUNT ON

IF @DocNumber Is Null
BEGIN
	EXEC [dbo].[GetNextDocNumber] 10, @intRet OUT
	SET @DocNumber = cast( @intRet as varchar(20))
END

IF @DocDate Is Null SET @DocDate = dbo.TodayDate() 
ELSE SET @DocDate = dbo.NormalizeDate(@DocDate)

IF @DepartmentUserId Is Null SELECT @DepartmentUserId = Id FROM CD.DepartmentUsers WHERE Login = suser_sname()

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
--получение значения идентификатора
EXEC @intRet = dbo.GetNextCounterFor 'Documents', @Id OUTPUT
IF @intRet <> 0 RETURN @intRet

	SET NOCOUNT OFF;
INSERT INTO [CA].[Documents] (Id, [DocTypeId], [DocDate], [DocNumber], DepartmentUserId, 
[Note], [MD], [MW]) 
VALUES (@Id, 10, @DocDate, @DocNumber, @DepartmentUserId, 
@Note, @MD, @MW);
RETURN 0
--select @Id = SCOPE_IDENTITY()
--SELECT Id, DepartmentId, DocTypeId, DocDate, DocNumber, 
--ContragentId, DepartmentUserId, Note, MD, MW 
--FROM CA.Documents WHERE Id = @Id